Skip to content

[RFC] Allow hooks for backed readonly properties #18757

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 13 commits into
base: master
Choose a base branch
from

Conversation

NickSdot
Copy link
Contributor

@NickSdot NickSdot commented Jun 4, 2025

@TimWolla TimWolla added the RFC label Jun 4, 2025
@NickSdot NickSdot force-pushed the allow-readonly-hooks branch 3 times, most recently from 1a21998 to 6840873 Compare June 6, 2025 02:22
@NickSdot NickSdot force-pushed the allow-readonly-hooks branch from da64264 to 24f2687 Compare June 7, 2025 16:23
@NickSdot NickSdot force-pushed the allow-readonly-hooks branch from c06c915 to 12b986a Compare June 7, 2025 16:28
@NickSdot NickSdot changed the title Allow hooks for backed readonly properties [RFC] Allow hooks for backed readonly properties Jun 7, 2025
@NickSdot
Copy link
Contributor Author

Larry (@Crell) correctly pointed out that the error messages for ZEND_ACC_IMPLICIT_ABSTRACT_CLASS and ZEND_ACC_EXPLICIT_ABSTRACT_CLASS also could be separated into two messages, instead of merging them into one.

Currently we have a single one for the both abstract types:

Hooked properties in abstract classes cannot be declared readonly

There could be two:

Hooked properties in abstract classes cannot be declared readonly
Abstract properties cannot be declared readonly

We would appreciate opinions on that! 🙂

cc @TimWolla, because this is a follow up to your "misleading errors" change request.

}
?>
--EXPECTF--
Fatal error: Hooked properties in abstract classes cannot be declared readonly in %s on line %d
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This one isn't better. My first review wasn't particularly precise in what was bothering me, though: I do not consider this to be a “hooked property”, since there is no actual hook. It just borrows the hook syntax to define the operations required by the interface, so calling this a “hook” is wrong in my book.

Just dropping the “Hooked” from the error message might be enough.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In fact, the correct error message in this case would be:

Fatal error: Non-abstract property hook must have a body in %s on line %d

Since that would be emitted when the readonly is removed. The readonly isn't the main problem here, the missing hook body is.

Copy link
Contributor Author

@NickSdot NickSdot Jun 22, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The readonly isn't the main problem here, the missing hook body is.

Not sure if I am following here. In 8.4 we have:

Hooked properties cannot be 'readonly'
Non-abstract property hook must have a body

In 8.5 we would have:
Hooked properties in abstract classes cannot be declared readonly
Non-abstract property hook must have a body

I feel like I am testing exactly what I should test. I also feel the new error is perfectly fine.

Copy link
Contributor

@Crell Crell Jun 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

An interface/abstract class declaring get or set doesn't imply hooks, just that those operations be valid. A normal, non-hooked property would satisfy that just as well.

I think the error messages should be something like:

interface I {
  public readonly $foo { get; }
}

"Interface properties may not be readonly."

abstract class A {
  abstract public readonly $foo { get; }
}

"Abstract properties may not be readonly."

class C {
  public readonly $foo { get; }
}

"Non-abstract property hook must have a body."

(Arguably we could combine the first two into a single message about abstract properties. I don't care much either way.)

Tim, would that work for you?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tim, would that work for you?

Yes. The important bit for me is the priority of the error messages. The missing body is the actual issue, not the readonly. This should likely also fixed in PHP 8.4, since it's confusing.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yes. The important bit for me is the priority of the error messages. The missing body is the actual issue, not the readonly. This should likely also fixed in PHP 8.4, since it's confusing.

I pushed an update.

Concern: To me it looks like Ilija did chose the previous order so that it would error out early before even looping over hooks->children. Now the loop runs always. Wdyt?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants